package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.TagsAdapter;
import com.example.myapplication.http.CommonCallback;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Objects;

public class TagsActivity extends AppCompatActivity {

    private AppCompatEditText keywordEditText = null;
    private AppCompatTextView searchTextView = null;
    private RecyclerView mainRecyclerView = null;
    private ArrayList<TagsData> arrayList = new ArrayList<>();
    private TagsAdapter mainAdapter = new TagsAdapter(arrayList);
    private AppCompatTextView clearTextView = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tags);
        keywordEditText = findViewById(R.id.keywordEditText);
        searchTextView = findViewById(R.id.searchTextView);
        mainRecyclerView = findViewById(R.id.mainRecyclerView);
        clearTextView = findViewById(R.id.clearTextView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mainRecyclerView.setLayoutManager(linearLayoutManager);
        mainRecyclerView.setAdapter(mainAdapter);

        mainAdapter.setOnItemClickListener(new TagsAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, TagsData bean) {
                Intent intent = new Intent();
                intent.putExtra("url", bean.getUrl());
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onDelete(int position, TagsData bean) {
                HttpParams httpParams = new HttpParams();
                httpParams.put("id", bean.getId());
                OkGo.<String>post(Common.API_URL + "member/delete/").tag(this).params(httpParams).execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String data = response.body();
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            String code = jsonObject.getString("code");
                            if (!code.equals("1")) {
                                String msg = jsonObject.getString("msg");
                                Toast.makeText(TagsActivity.this, msg, Toast.LENGTH_SHORT).show();
                                return;
                            }
                            Toast.makeText(TagsActivity.this, "删除成功~", Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

        searchTextView.setOnClickListener(view -> {
            String keyword = Objects.requireNonNull(keywordEditText.getText()).toString();
            HttpParams httpParams = new HttpParams();
            httpParams.put("keyword", keyword);
            MainActivity.commonHttp.post("member/getTags/", httpParams, new CommonCallback() {
                @Override
                public void onSuccess(String data) {
                    try {
                        arrayList.clear();
                        TagsData tagsData = null;
                        JSONArray jsonArray = new JSONArray("data");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            tagsData = new TagsData();
                            tagsData.setId(jsonObject.getString("tags_id"));
                            tagsData.setTitle(jsonObject.getString("tags_title"));
                            tagsData.setUrl(jsonObject.getString("tags_link"));
                            arrayList.add(tagsData);
                        }
                        mainAdapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(String msg) {

                }
            });
        });

        HttpParams httpParams = new HttpParams();
        OkGo.<String>post(Common.API_URL + "member/getTags/").tag(this).params(httpParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String data = response.body();
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    String code = jsonObject.getString("code");
                    if (!code.equals("1")) {
                        String msg = jsonObject.getString("msg");
                        Toast.makeText(TagsActivity.this, msg, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //处理数据
                    arrayList.clear();
                    TagsData tagsData = null;
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        jsonObject = jsonArray.getJSONObject(i);
                        tagsData = new TagsData();
                        tagsData.setId(jsonObject.getString("tags_id"));
                        tagsData.setTitle(jsonObject.getString("tags_title"));
                        tagsData.setUrl(jsonObject.getString("tags_link"));
                        arrayList.add(tagsData);
                    }
                    mainAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        clearTextView.setOnClickListener(view -> {
            //请求接口，实现清除书签功能
            OkGo.<String>post(Common.API_URL + "member/clear/").tag(this).execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    String data = response.body();
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        String code = jsonObject.getString("code");
                        if (!code.equals("1")) {
                            String msg = jsonObject.getString("msg");
                            Toast.makeText(TagsActivity.this, msg, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //处理数据
                        Toast.makeText(TagsActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        });

    }

}
